package com.example.model.title.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.*;
import com.example.auth.model.UserModel;
import com.example.auth.service.UserModelService;
import com.example.db.model.IPage;
import com.example.model.title.model.ChannelModel;
import com.example.model.title.model.ContentModel;
import com.example.model.title.model.TitleModel;
import com.example.model.title.model.TitleModelExt;
import com.example.model.title.model.vo.TitleVo;
import com.example.model.title.service.ChannelModelService;
import com.example.model.title.service.ContentModelService;
import com.example.model.title.service.TitleModelService;
import com.example.utils.Results;
import com.example.utils.StringUtils;

import java.util.Date;
import java.util.jar.Pack200;

/**
 * @Classname TitleController
 * @Description TODO
 * @Date 2022/8/7 1:18
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController("/title")
public class TitleController {


    @Autowired
    TitleModelService titleModelService;

    @Autowired
    ContentModelService contentModelService;

    @Autowired
    ChannelModelService channelModelService;

    @Autowired
    UserModelService userModelService;

    @PostMapping("/page")
    public JSONObject pageControler(@Param(value = "pageNum",desc = "页码",defalultValue = "1")Integer pageNum,
                                    @Param(value = "pageSize",desc = "条数",defalultValue = "10")Integer pageSize,
                                    @Param(value = "searchParams",required = false)JSONObject object){
        IPage<TitleModelExt> iPage=titleModelService.page(pageNum,pageSize,object);
        return Results.successLayuiTable(iPage);
    }

    @PostMapping("/save")
    public JSONObject saveController(@RequestBody TitleVo titleVo){
        UserModel userModel=userModelService.getUserModel();
        if(titleVo == null){
            return Results.error("必要信息不得为空");
        }
        if(titleVo.getChannelId()==null || titleVo.getChannelIdName()==null){
            return Results.error("未选择栏目");
        }
        ChannelModel channelModel=channelModelService.getId(titleVo.getChannelId());
        if(channelModel == null){
            return Results.error(titleVo.getChannelIdName()+"栏目不存在");
        }
        if(titleVo.getId()==null){
            ContentModel contentModel=new ContentModel();
            contentModel.setContent(titleVo.getContent());
            contentModelService.save(contentModel);
            TitleModel titleModel = new TitleModel();
            titleModel.setTitle(titleVo.getTitle());
            titleModel.setTag(titleVo.getTag());
            titleModel.setChannelId(titleVo.getChannelId());
            titleModel.setContentid(contentModel.getId());
            titleModel.setCreatedate(new Date());
            titleModel.setUid(userModel.getId());
            titleModel.setIsmarker(titleVo.isIsmarker());
            titleModelService.save(titleModel);
            return Results.success("新增成功",titleModel);
        }

        TitleModel titleModel=titleModelService.getId(titleVo.getId());
        if(titleModel==null){
            return Results.error("系统错误");
        }
        ContentModel contentModel=contentModelService.getId(titleModel.getContentid());
        if(contentModel == null) {
            contentModel=new ContentModel();
            contentModel.setContent(titleVo.getContent());
            contentModelService.save(contentModel);
            titleModel.setContentid(contentModel.getId());
        }else {
            contentModel.setContent(titleVo.getContent());
            contentModelService.update(contentModel);
        }
        if(!StringUtils.isEmpty(titleVo.getChannelId())){
            titleModel.setChannelId(titleVo.getChannelId());
        }
        if (!StringUtils.isEmpty(titleVo.getTag())) {
            titleModel.setTag(titleVo.getTag());
        }
        if (!StringUtils.isEmpty(titleVo.getTitle())) {
            titleModel.setTitle(titleVo.getTitle());
        }
        if(StringUtils.isEmpty(titleVo.isIsmarker())){
            titleModel.setIsmarker(titleVo.isIsmarker());
        }
        titleModel.setChannelId(titleVo.getChannelId());
        titleModelService.update(titleModel);
       return Results.success("修改成功");

    }

    @PostMapping("/update")
    public JSONObject updateController(@RequestBody TitleModel titleModel){
        titleModelService.update(titleModel);
        return Results.success("更新成功");
    }

    @GetMapping("/getone")
    public JSONObject getOneController(@Param("id")String id){
        TitleModel titleModel=titleModelService.getId(id);
        return Results.success(titleModel);
    }

    @GetMapping("/getextone")
    public JSONObject getExtOneController(@Param("id")String id){
        TitleModelExt titleModelExt=titleModelService.getTitleModelExtById(id);
        return Results.success(titleModelExt);
    }
}
